home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / BLKIO.H < prev    next >
Text File  |  1991-09-23  |  5KB  |  157 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /*man---------------------------------------------------------------------------
  5. NAME
  6.      blkio - block buffered input/output library
  7.  
  8. SYNOPSIS
  9.      #include <blkio.h>
  10.  
  11. DESCRIPTION
  12.      blkio is a buffered input/output library for structured files.
  13.      Because structured files are primarily accessed randomly rather
  14.      than sequentially, they are better modeled as collections of
  15.      blocks rather than as streams of characters.  This library may be
  16.      used with files which fit the following criteria:
  17.  
  18.           o A header of arbitrary (possibly zero) but fixed
  19.             length appears at the beginning of the file.
  20.           o The data following the header is arranged in
  21.             blocks of uniform size.
  22.  
  23.      Files fitting this model are referred to in the documentation as
  24.      block files.
  25.  
  26.      A file to be accessed with the blkio library is declared to be a
  27.      pointer to a defined type BLKFILE.  The bopen function creates
  28.      certain descriptive data for the file and returns a pointer to
  29.      designate it in all further transactions.
  30.  
  31. SEE ALSO
  32.      bclose, bcloseall, bexit, bflpop, bflpush, bflush, bgetb, bgetbf,
  33.      bgeth, bgethf, bopen, bputb, bputbf, bputh, bputhf, bsetbuf,
  34.      bsetvbuf, bsync, lockb.
  35.  
  36. ------------------------------------------------------------------------------*/
  37. #ifndef H_BLKIO        /* prevent multiple includes */
  38. #define H_BLKIO
  39.  
  40. /* #ident    "@(#)blkio.h    1.5 - 91/09/23" */
  41.  
  42. #include <ansi.h>
  43.  
  44. /* ansi headers */
  45. #ifdef AC_STDDEF
  46. #include <stddef.h>
  47. #endif
  48. #include <stdio.h>
  49.  
  50. /* constants */
  51. #define BOPEN_MAX    (FOPEN_MAX > 60 ? FOPEN_MAX : 60)
  52.                     /* max # block files open at once */
  53. #define NIL        ((bpos_t)0)    /* nil file pointer */
  54. #define NUL        ('\0')        /* nul char */
  55.  
  56. /* macro for number elements in a static array */
  57. #define nelems(v)    (sizeof((v)) / sizeof(*(v)))
  58.  
  59. /* macro for sizeof a structure member */
  60. #define sizeofm(struct_t, member) ((size_t)(sizeof(((struct_t *)0)->member)))
  61.  
  62. /* type definitions */
  63. typedef unsigned long    bpos_t;    /* block file position */
  64.  
  65. typedef union {            /* file desciptor type */
  66.     char    c;        /* character file descriptor */
  67.     short    s;        /* short int file descriptor */
  68.     int    i;        /* int file descriptor */
  69. } fd_t;
  70.  
  71. typedef struct {        /* block structure */
  72.     bpos_t    bn;        /* block number */
  73.     int    flags;        /* block status flags */
  74.     size_t    more;        /* link to more recently accessed block */
  75.     size_t    less;        /* link to less recently accessed block */
  76. } block_t;
  77.  
  78. typedef struct {        /* block file control structure */
  79.     fd_t    fd;        /* file descriptor for buffered file */
  80.     int    flags;        /* buffer status flags */
  81.     size_t    hdrsize;    /* size of file header */
  82.     size_t    blksize;    /* size of blocks */
  83.     size_t    bufcnt;        /* number blocks to buffer (0 if unbuffered) */
  84.     bpos_t    endblk;        /* first block past end of file */
  85.     size_t    most;        /* most recently accessed block [1..bufcnt] */
  86.     size_t    least;        /* least recently accessed block [1..bufcnt] */
  87.     block_t *blockp;    /* doubly linked list of blocks */
  88.     void *    blkbuf;        /* buffer storage for header and blocks */
  89. } BLKFILE;
  90.  
  91. /* function declarations */
  92. #ifdef AC_PROTO
  93. int        bclose(BLKFILE *bp);
  94. void        bcloseall(void);
  95. void        bexit(int status);
  96. int        bflpop(BLKFILE *bp, bpos_t *bnp);
  97. int        bflpush(BLKFILE *bp, const bpos_t *bnp);
  98. int        bflush(BLKFILE *bp);
  99. int        bgetb(BLKFILE *bp, bpos_t bn, void *buf);
  100. int        bgetbf(BLKFILE *bp, bpos_t bn, size_t offset,
  101.             void *buf, size_t bufsize);
  102. int        bgeth(BLKFILE *bp, void *buf);
  103. int        bgethf(BLKFILE *bp, size_t offset, void *buf, size_t bufsize);
  104. BLKFILE *    bopen(const char *filename, const char *type,
  105.             size_t hdrsize, size_t blksize, size_t bufcnt);
  106. int        bputb(BLKFILE *bp, bpos_t bn, const void *buf);
  107. int        bputbf(BLKFILE *bp, bpos_t bn,
  108.             size_t offset, const void *buf, size_t bufsize);
  109. int        bputh(BLKFILE *bp, const void *buf);
  110. int        bputhf(BLKFILE *bp, size_t offset,
  111.             const void *buf, size_t bufsize);
  112. int        bsetbuf(BLKFILE *bp, void *buf);
  113. int        bsetvbuf(BLKFILE *bp, void *buf, size_t blksize, size_t bufcnt);
  114. int        bsync(BLKFILE *bp);
  115. int        lockb(BLKFILE *bp, int ltype, bpos_t start, bpos_t len);
  116. #else
  117. int        bclose();
  118. void        bcloseall();
  119. void        bexit();
  120. int        bflpop();
  121. int        bflpush();
  122. int        bflush();
  123. int        bgetb();
  124. int        bgetbf();
  125. int        bgeth();
  126. int        bgethf();
  127. BLKFILE *    bopen();
  128. int        bputb();
  129. int        bputbf();
  130. int        bputh();
  131. int        bputhf();
  132. int        bsetbuf();
  133. int        bsetvbuf();
  134. int        bsync();
  135. int        lockb();
  136. #endif    /* #ifdef AC_PROTO */
  137.  
  138. /* lock types */
  139. #define B_UNLCK        (0)    /* unlock */
  140. #define B_RDLCK        (1)    /* read lock */
  141. #define B_WRLCK        (2)    /* write lock */
  142. #define B_RDLKW        (3)    /* read lock, wait */
  143. #define B_WRLKW        (4)    /* write lock, wait */
  144.  
  145. /* error codes */
  146. #define BEOS        (0)        /* start of blkio error code domain */
  147. #define BEMFILE        (BEOS - 1)    /* too many block files open */
  148. #define BENOPEN        (BEOS - 2)    /* block file is not open */
  149. #define BENBUF        (BEOS - 3)    /* buffering is off */
  150. #define BEBUF        (BEOS - 4)    /* buffering is on */
  151. #define BEBOUND        (BEOS - 5)    /* block boundary error */
  152. #define BEEOF        (BEOS - 6)    /* past end of file */
  153. #define BENFL        (BEOS - 7)    /* no free list */
  154. #define BEPANIC        (BEOS - 8)    /* internal blkio error */
  155.  
  156. #endif    /* #ifndef H_BLKIO */
  157.